package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsGrouponRules;
import com.qf.service.AdminGrouponService;
import com.qf.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;


@RequestMapping("/admin/groupon")
@CrossOrigin
@RestController
public class AdminGrouponController {

    @Autowired
    private AdminGrouponService adminGrouponService;

    /**
     * 查询团购列表
     * @param page
     * @param limit
     * @param goodsId
     * @param sort
     * @param order
     * @return
     */
    @GetMapping("/list")
    @RequiresPermissions("admin:groupon:list")
    @RequiresPermissionsDesc(menu = {"推广管理","团购规则"},button = "团购列表")
    public Object grouponList(@RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "20")Integer limit,
                              Integer goodsId,String sort,String order){
        IPage<DtsGrouponRules> iPage = adminGrouponService.selectGrouponList(page,limit,goodsId,sort,order);

        long total = iPage.getTotal();
        List<DtsGrouponRules> records = iPage.getRecords();

        DtsGrouponRules[] items = new DtsGrouponRules[records.size()];

        for (int i = 0; i < records.size(); i++) {
            items[i] = records.get(i);
        }

        HashMap<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", items);

        return ResponseUtil.ok(data);
    }

    /**
     * 团购添加
     * @param dtsGrouponRules
     * @return
     */
    @PostMapping("/create")
    @RequiresPermissions("admin:groupon:create")
    @RequiresPermissionsDesc(menu = {"推广管理","团购规则"},button = "团购添加")
    public Object grouponCreate(@RequestBody DtsGrouponRules dtsGrouponRules){
        DtsGrouponRules data = adminGrouponService.insertGroupon(dtsGrouponRules);
        return ResponseUtil.ok(data);
    }

    //团购修改
    @PostMapping("/update")
    @RequiresPermissions("admin:groupon:update")
    @RequiresPermissionsDesc(menu = {"推广管理","团购规则"},button = "团购修改")
    public Object grouponUpdate(@RequestBody DtsGrouponRules dtsGrouponRules){

        adminGrouponService.updateGroupon(dtsGrouponRules);
        return ResponseUtil.ok();
    }

    //团购删除
    @PostMapping("/delete")
    @RequiresPermissions("admin:groupon:delete")
    @RequiresPermissionsDesc(menu = {"推广管理","团购规则"},button = "团购删除")
    public Object grouponDelete(@RequestBody DtsGrouponRules dtsGrouponRules){
        adminGrouponService.deleteGroupon(dtsGrouponRules.getId());
        return ResponseUtil.ok();
    }

}
